//引入
const express = require('express');
const db = require('../../utils/db')

//配置路由器
const gonggao = express.Router();

//接口编写

//获取
gonggao.get('/gonggao/query', (req, res) => {
  const page = req.query.page;
  const count = req.query.count;
  const sql = `select * from g_gonggao limit ${(page - 1) * count},${count}`;
  const sql2 = `select count(*) as total from g_gonggao;`;
  db.query(sql, (err, data) => {
    if (err) {
      console.log(err)
      res.send({ error: 1, msg: err })
    } else {
      db.query(sql2, (err2, data2) => {
        if (err2) {
          console.log(err2)
          res.send({ error: 1, msg: err2 })
        } else {
          console.log(data2)
          res.send({ error: 0, msg: '查询成功', data: data, total: data2[0].total })
        }
      })
    }
  })
})
//添加
gonggao.post('/gonggao/add', (req, res) => {
  const sql = `insert into g_gonggao values(null,'${req.body.g_biaoti}','${req.body.g_name}','${req.body.g_date}','${req.body.g_lanmu}','${req.body.g_jieshou}','${req.body.g_tixing}')`;

  db.query(sql, (err, data) => {
    if (err) {
      console.log(err)
      res.send({ error: 1, msg: err })
    } else {
      res.send({ error: 0, msg: '添加成功', data: data })
    }
  })
})
//查询
gonggao.post('/gonggao/sousuo', (req, res) => {
  const sql = `SELECT * FROM g_gonggao WHERE  g_name='${req.body.g_name}' OR g_lanmu='${req.body.g_lanmu}'  OR g_biaoti LIKE'%${req.body.g_biaoti}%' ;`
  db.query(sql, (err, data) => {
      if (err) {
          console.log(err);
          res.send({ error: 1, msg: err })
      } else {
          res.send({ error: 0, msg: '查询成功', data: data })
      }
  })
})
// 删除
gonggao.post('/gonggao/del', (req, res) => {
  const sql = 'delete from g_gonggao where g_id="' + req.body.id + '";';
  db.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.send({ error: 1, msg: err })
    } else {
      res.send({ error: 0, msg: '删除成功' })
    }
  })
})
//导出
module.exports = gonggao;